package be.kuleuven.cs.mop.gui.components;


import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;



public class Spinner extends JSpinner {
	private static final long serialVersionUID = 1L;
	
	
	public Spinner(final int n) {
		super(new SpinnerNumberModel(0, 0, (n - 1), 1));
		setEditor(new Editor(this));
		setFocusable(false);
		setPreferredSize(new Dimension(54,25));
	}
	
	
	
	public int read() {
		return ((Integer)getValue()).intValue();
	}
	
	
	
	
	
	
	
	
	
	
	private static final class Editor extends JTextField implements ChangeListener {
		private static final long serialVersionUID = 1L;
		
		
		private Editor(final Spinner component) {
			super(Integer.toString(component.read()));
			setEditable(false);
			setFocusable(false);
			setFont(new Font("Helvetica", Font.BOLD, 14));
			setHorizontalAlignment(SwingConstants.CENTER);
			
			component.addChangeListener(this);
		}
		
		
		
		@Override
		public final void stateChanged(final ChangeEvent event) {
			setText(Integer.toString(((Spinner)event.getSource()).read()));
		}
		
	}
	
}